package com.cyx.exam.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyx.exam.entity.Exam;
import com.cyx.exam.entity.Qu;
import com.cyx.exam.entity.QuAnswer;
import com.cyx.exam.entity.Result;
import com.cyx.exam.service.ExamService;
import com.cyx.exam.service.QuAnswerService;
import com.cyx.exam.service.QuService;
import com.cyx.exam.vo.QuVO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 问题题目 前端控制器
 * </p>
 *
 * @author cyx
 * @since 2021-03-09
 */
@Controller
@RequestMapping("/qu")
public class QuController {

    @Resource
    private QuService quService;

    @Resource
    private ExamService examService;

    @Resource
    private QuAnswerService quAnswerService;

    //进入编辑页面
    @PostMapping("/edit")
    public String edit(Exam exam, Model model){
        examService.saveOrUpdate(exam);
        model.addAttribute("examId",exam.getId());
        return "redirect:/qu/show";
    }


    //进入新增编辑页面
    @GetMapping("/add/{examId}")
    public String add(@PathVariable(value = "examId") Integer examId,
                       Model model){
       model.addAttribute("examId",examId);
       return "qu/add";
    }

    //进入修改编辑页面
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable(value = "id") Integer id,
                       Model model){
        //1.查找题目
        Qu qu = quService.getById(id);
        //2.查找相关答案
        QueryWrapper<QuAnswer> quAnswerWrapper = new QueryWrapper<>();
        quAnswerWrapper.eq("qu_id",qu.getId());
        List<QuAnswer> quAnswerList = quAnswerService.list(quAnswerWrapper);
        //3.赋值给QuVO
        QuVO quVO = new QuVO();
        quVO.setId(qu.getId())
                .setQuType(qu.getQuType())
                .setContent(qu.getContent())
                .setRemark(qu.getRemark())
                .setAnalysis(qu.getAnalysis())
                .setScore(qu.getScore())
                .setExamId(qu.getExamId())
                .setQuAnswerList(quAnswerList);
        model.addAttribute("quVO",quVO);
        model.addAttribute("examId",qu.getExamId());
        return "qu/edit";

    }

    //展示试卷下的全部题目
    @GetMapping("/list/{id}")
    public String show(@PathVariable("id") Integer examId, Model model){
        List<QuVO> quVOList = new ArrayList<>();

        QueryWrapper<Qu> quWrapper = new QueryWrapper<>();
        quWrapper.eq("exam_id",examId);
        List<Qu> quList = quService.list(quWrapper);

        //找出题目和相应的选项，赋值给quVOList
        for(Qu qu : quList){
            QueryWrapper<QuAnswer> quAnswerWrapper = new QueryWrapper<>();
            quAnswerWrapper.eq("qu_id",qu.getId());
            List<QuAnswer> quAnswerList = quAnswerService.list(quAnswerWrapper);
            quVOList.add(new QuVO().setId(qu.getId())
                                    .setQuType(qu.getQuType())
                                    .setContent(qu.getContent())
                                    .setRemark(qu.getRemark())
                                    .setAnalysis(qu.getAnalysis())
                                    .setScore(qu.getScore())
                                    .setExamId(qu.getExamId())
                                    .setQuAnswerList(quAnswerList));
        }
        model.addAttribute("quVOList",quVOList);
        model.addAttribute("examId",examId);
        return "qu/list";
    }
    //删除题目
    @GetMapping("/del/{examId}/{quId}")
    @Transactional
    public String delete(@PathVariable(value = "examId") Integer examId,
                         @PathVariable(value = "quId") Integer quId){
        quService.deleteById(quId);
        quAnswerService.deleteByQuId(quId);
        return "redirect:/qu/list/"+examId;
    }

    //对题目进行修改操作
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody QuVO quVO){
        Qu qu = new Qu();
        qu.setId(quVO.getId())
               .setQuType(quVO.getQuType())
               .setContent(quVO.getContent())
               .setRemark(quVO.getRemark())
               .setAnalysis(quVO.getAnalysis())
               .setScore(quVO.getScore())
               .setExamId(quVO.getExamId());
        //修改题目
        quService.saveOrUpdate(qu);
        //找出数据库中的选项
        QueryWrapper<QuAnswer> wrapper = new QueryWrapper<>();
        wrapper.eq("qu_id",qu.getId());
        List<QuAnswer> deleteList = quAnswerService.list(wrapper);
        //找出已经删除的选项
        for (int i=0;i<deleteList.size();i++) {
            for (QuAnswer quAnswer:quVO.getQuAnswerList()) {
                if (deleteList.get(i).getId()==quAnswer.getId()){
                    deleteList.remove(i);
                    i--;
                    break;
                }
            }
        }
        //对不存在的项进行删除
        for (QuAnswer quAnswer:deleteList) {
            quAnswerService.delete(quAnswer.getId());
        }

        //其他选项进行新增或更新
        quAnswerService.saveOrUpdateBatch(quVO.getQuAnswerList());
        System.out.println("成功");p:
        return new Result("200","/qu/list/"+quVO.getExamId());
//        return "redirect:/qu/list/"+quVO.getExamId();
    }

    //对题目进行新增操作
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody QuVO quVO){
        Qu qu = new Qu();
        qu.setId(quVO.getId())
                .setQuType(quVO.getQuType())
                .setContent(quVO.getContent())
                .setRemark(quVO.getRemark())
                .setAnalysis(quVO.getAnalysis())
                .setScore(quVO.getScore())
                .setExamId(quVO.getExamId());
        quService.save(qu);
        //对选项进行新增或更新
        for (QuAnswer quAnswer:quVO.getQuAnswerList()){
            quAnswer.setQuId(qu.getId());
            quAnswerService.save(quAnswer);
        }
        return new Result("200","/qu/list/"+quVO.getExamId());
//        return "redirect:/qu/list/"+quVO.getExamId();
    }
}

